package ru.papergames.view.components
{
	import caurina.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	

	public class AlertText extends Sprite
	{
		
		public static const INFO:String = 'info';
		public static const WARNING:String = 'warning';
		public static const ERROR:String = 'error';
		
		private static const TYPE_COLORS:Object = {	info: 0x229922,
													warning: 0x999900,
													error: 0xFF3300};
													
													
		private static const TEXT_FILTER:GlowFilter = new GlowFilter(0, 0.8, 3, 3, 4, 2);
		
		public static function show(parentContainer:DisplayObjectContainer, text:String, type:String = 'info', persist:Boolean = false):void {
									
			var faText:AlertText = new AlertText(text, type);
			faText.y = 10;
			faText.x = parentContainer.width/2;
			
			faText.persist = false;// persist;
			
			//shift previous!			
			var shift:Number = 40;			
			
			for (var i:uint = 0; i < parentContainer.numChildren; i++ ) {
				var clip:DisplayObject = parentContainer.getChildAt(i);
				
				if (clip is AlertText) {
					
					Tweener.addTween(clip, {y: clip.y + shift,
											time: 0.2,
											transition: 'easeOutCubic'}
											);
					
				}
			}
			
			parentContainer.addChild(faText);
			
		}
		
		
		protected var persist:Boolean = false;
		
		
		public function AlertText(text:String, type:String)
		{
			super();
			this.filters = [TEXT_FILTER];
			this.mouseEnabled = false;
			this.mouseChildren = false;
			var tf:TextField = new TextField();
			tf.selectable = false;
			
			var tFormat:TextFormat = new TextFormat();
			tFormat.color = tf.textColor = TYPE_COLORS[type];
			tFormat.size = 14;
			tFormat.bold = true;
			tFormat.font = 'Tahoma';
			tFormat.align = TextFormatAlign.CENTER;
									
			tf.defaultTextFormat = tFormat;
			
			tf.text = text.toLocaleUpperCase();	
			tf.width = tf.textWidth + 10;
			
			this.addChild(tf);
			
			tf.x = -tf.textWidth/2;
		
			this.alpha = 0;	
			//this.scaleX = this.scaleY = 0.5;
			
			
			//draw rect
			var g:Graphics = this.graphics;
			
			g.lineStyle(1, 0);
						
			g.beginFill(0xFFFFFF, 1);
				g.drawRect(tf.x - 5, -2, tf.width+10, tf.textHeight + 10);			
			g.endFill();
			
			
			Tweener.addTween(this, {alpha: 1,
									scaleX: 1,
									scaleY: 1,
									time: 0.2,
									transition: 'easeInCubic', 
									onComplete: onHide}
									);	
		}
		
		private function onHide():void {
			
			if (persist) return;
			
			Tweener.addTween(this, {alpha: 0, 
									scaleX: 0.3,
									scaleY: 0.3,
									delay: 1 + this.width/600,
									time: 0.5,
									transition: 'easeOutCubic', 
									onComplete: onRemove}
									);
		}
		
		private function onRemove():void {
			if (parent) parent.removeChild(this);
		}
		
	}
}